<?php
	class LoginView {
		private $m_getPassword = "password";
		private $m_getUsername = "username";
		private $m_getRememberMe = "checkbox";
		private $m_loginButton = "loginbutton";
		private $m_logoutButton = "logoutbutton";
		public $m_usernameCookie = "user";
		public $m_passwordCookie = "pw";
		
		
		/**
		 * @return string HTML loginBox
		 */
		public function DoLoginBox() {
			return "<div>
						<form method='post'>
							<fieldset>
								<label for='$this->m_getUsername'>Username: </label>
								<input name='$this->m_getUsername' type='text'/><br />
								<label for='$this->m_getPassword'>Password: </label>
								<input name='$this->m_getPassword' type='text'/><br />
								<input name='$this->m_loginButton' type='submit' value='Login'> 
								<label for='$this->m_getRememberMe'>Remember me </label>
								<input name='$this->m_getRememberMe' type='Checkbox' />
							</fieldset>
						</form>
					</div>";
		}
		
		/**
		 * @return string HTML logoutBox
		 */
		public function DoLogoutBox() {
			return "<div>
						<form method='post'>
							<fieldset>
								<input type='submit' name='$this->m_logoutButton' value='Logout'/>
							</fieldset>
						</form>
					</div>";
		}
		
		/**
		 * Gets username from input data
		 * @return string Username from input
		 */
		public function GetUserName() {
			if(isset($_POST[$this->m_getUsername])) {
				return $_POST[$this->m_getUsername];
			}
			return NULL;
		}
		
		/**
		 * Gets password from input data
		 * @return string Password from input
		 */
		public function GetPassword() {
			if(isset($_POST[$this->m_getPassword])) {
				return $_POST[$this->m_getPassword];
			}
			return NULL;
		}
		
		/**
		 * Login success
		 * @return string html
		 */
		public function SuccessfullLogin($user, $pw) {
			if ($this->GetRememberMe()) {
				$this->SetCookie($this->m_usernameCookie, $user);
				$this->SetCookie($this->m_passwordCookie, $pw);
			}
			return "<br />Successfull Login.";
		}
		
		/**
		 * Failed to login
		 * @return string html
		 */
		public function failedLogin() {
			return "<br />Wrong password or username.";
		}
		
		/**
		 * Logsout the user
		 */
		public function SuccessfullLogout($lh) {
		 	unset($_SESSION[$lh->m_login]);
			$this->KillCookie($this->m_usernameCookie);
			$this->KillCookie($this->m_passwordCookie);
		 }
		
		/**
		 * @return boolean
		 */
		public function GetRememberMe(){
			if(isset($_POST[$this->m_getRememberMe])) {
				return true;
			}
			return false;
		}
		
		/**
		 * @param string $cookie
		 * @param $value
		 */
		public function SetCookie($cookie, $value) {
			setcookie($cookie, $value, time()+3600);
		}
		
		/**
		 * @param string $cookie
		 */
		public function KillCookie($cookie) {
			setcookie($cookie, "", time()-3600);
		}
		
		/**
		 * @param string $cookie
		 * @return $value
		 */
		public function GetCookie($cookie) {
			if(isset($_COOKIE[$cookie])) {
				return $_COOKIE[$cookie];
			}
			return null;
		}
		
		/**
		 * @param int $errorCode
		 * @return string $html
		 */
		public function ErrorHandler($errorCode) {
			switch ($errorCode) {
				case fileUploadHandler::EVERYTHING_WENT_BETTER_THEN_EXPECTED:
						$html = "File upload success!";
					break;
				case fileUploadHandler::GENERIC_ERROR:
						$html = "Error: Upload failed.";
					break;
				case fileUploadHandler::EXTENSION_NOT_SUPPORTED:
						$html = "Error: That exstension is not supported.";
					break;
				case fileUploadHandler::FAILED_TO_MOVE:
						$html = "Error: Failed to move file.";
					break;	
			}
			return $html;
		}
		
		/**
		 * Gets loginButton from input data
		 * @return boolean loginButton from input data
		 */
		public function TriedToLogin() {
			if(isset($_POST[$this->m_loginButton])) {
				return true;
			}
			return false;
		}
		
		/**
		 * Gets logoutButton from input data
		 * @return boolean logoutButton from input data
		 */
		public function TriedToLogout() {
			if(isset($_POST[$this->m_logoutButton])) {
				return true;
			}
			return false;
		}
		
		/**
		 * Echo test data
		 * @return boolean
		 */
		public function Test() {
			
			$this->KillCookie("test");
						
			$this->SetCookie("test", 12);
			
			if ($this->GetCookie("test") != 12) {
				echo "Något gick galet.";
				return false;
			}

			$this->KillCookie("test");
			
			return true;
		}
	}
?>